import requests

from mcp.server.fastmcp import FastMCP
#实例化MCP服务对象
mcp = FastMCP("Weather MCP Server")

@mcp.resource(
    uri="file:///weather_code.txt",
    name="weather_code",
    description="根据城市的名称查询对应的地区编码 adcode和城市编码 citycode",
    mime_type="text/plain",
)
def weather_code()->str:
    """
    返回所有的城市的名称，地区编码 adcode,城市编码 citycode
    :return: 名称，地区编码 adcode,城市编码 citycode
    """
    with open("weather_code.data", "r",encoding="utf-8") as file:
        content = file.read()
    #返回查询的内容
    return content

@mcp.tool()
def query_weather(adcode: str)->str:
    """
    根据城市的地区编号查询对应的天气信息
    :param adcode: 地区编号
    :return: 天气信息
    """
    url = f"https://restapi.amap.com/v3/weather/weatherInfo?key=d64c259dde4173ce7be767be32eb2bb9&city={adcode}&extensions=all"
    response = requests.get(url)
    if response.status_code == 200:
        data = response.json()
        # 处理返回的数据
        print("查询API接口返回的数据：",data)
        return data
    else:
        print("查询API接口失败")
        return "查询API接口失败"


if __name__ == "__main__":
    print(" MCP Server Start!")
    #启动mcp服务:有两种协议，分别是stdio和tcp，stdio模式下，transport参数必须为stdio
    mcp.run(transport="sse")